package com.zang.im.croom.netty.common;

import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Global 全局
 *
 * @author Zhang Qiang
 * @Date 2019/7/23 17:52
 */
@Component
public class SSGlobalConstMap {
     /**
      * 可见且有序
      */
    private volatile static Map<String, Channel> channelMap ;

    private SSGlobalConstMap(){}

    public static Map<String, Channel> getChannelMap(){
        if (channelMap == null){
            synchronized (SSGlobalConstMap.class){
                if (channelMap == null){
                    channelMap = new ConcurrentHashMap<String, Channel>();
                }
            }
        }
        return channelMap;
    }

    public static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    int getChannelTotal(){
        return channelMap.size();
    }

}
